<template>
  <div>
    <el-dialog title="信息" :visible.sync="dialogShow" width="70%" :close-on-click-modal="false" :close-on-press-escape="false" :append-to-body="true">
      <el-form :model="formObj" ref="form" label-width="110px" size="small" label-position="left">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="描述" prop="message">
              <el-input type="textarea" :autosize="{ minRows: 3, maxRows: 6 }" :rows="3" v-model="formObj.commentContent" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <file-view ref="fileView" :isComment="true"></file-view>

      <div slot="footer" class="dialog-footer margin-bottom-40">
        <el-button size="mini" class="f-right el-button-reset" type="primary" @click="hideDialog">取 消</el-button>
        <el-button size="mini" class="f-right margin-right-15" type="primary" @click="sendMsg">发 送</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { apiCommentList, apiComment, apiCommentReply } from '../../../api/workTrace';
  import { requestTips, askDialog, formValid } from '@/common/js/utils';

  import fileView from './fileView.vue';

  export default {
    name: 'addWork',
    props: {
      isLeader: {
        type: Boolean,
        default: false,
      },

      model: {
        type: Object,
        default: () => {
          return {};
        },
      },

      //留言:levaeMsh 汇报:report 回复：reply
      funcType: {
        type: String,
        default: '',
      },
    },
    data() {
      return {
        dialogShow: false,
        userInfo: JSON.parse(localStorage.getItem('userInfo')),
        formObj: {
          commentContent: '',
        },
      };
    },

    methods: {
      async init(){
        this.dialogShow = true;
        this.formObj.commentContent = ""
        await this.$nextTick()
        this.$refs.form.clearValidate()
        this.$refs.fileView.init()
      },

      hideDialog() {
        this.dialogShow = false;
      },

      async sendMsg() {
        if (this.funcType == "reply") {
          this.replyFn()
        } else {
          formValid(this, 'form', this.commentFn)
        }
      },

      //回复
      async replyFn() {
        if (!this.formObj.commentContent) {
          this.$message.warning("请输入描述")
          return
        }
        let res = await apiCommentReply({
          commentKey: this.model.primaryKey, //评论的key
          replyType: 0,
          parentUserKey: this.model.userKey, //父级的key
          parentKey: this.model.primaryKey,
          replyContent: this.formObj.commentContent,
          replyUserKey: this.userInfo.key,
        });
        requestTips(res, async () => {
          this.dialogShow = false
          this.$emit("refresh")
        });
      },

      //评论
      async commentFn() {
        if (!this.formObj.commentContent) {
          this.$message.warning("请输入描述")
          return
        }
        let res = await apiComment({
          bizType: 'work',
          userKey: this.userInfo.key,
          bizKey: this.model.primaryKey,
          commentContent: this.formObj.commentContent,
          commentType:this.funcType == 'report' ? 1 : 2,
          imageAttachmentList: this.$refs.fileView.getPhotoList(),
          fileAttachmentList: this.$refs.fileView.getFileCommentList(),
        });
        requestTips(res, async () => {
          this.dialogShow = false
          this.$emit("refresh")
        });
      },
    },

    components: { fileView },
  };
</script>

<style lang="scss" scoped></style>
